# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import os


class WwwscrapyPipeline(object):
    def process_item(self, item, spider):
        path = item.get("path")

        ret = path.replace("https://www.osgeo.cn/", "")

        dirpath = ret[:ret.rfind("/")]

        if not os.path.exists("./" + dirpath):
            os.makedirs("./" + dirpath)

            print(dirpath, "创建")

        print(ret)
        with open("./" + ret, "w+", encoding="utf-8") as f:
            f.write(item.get("data"))

            print(ret, "写入成功")
        return item
